package com.angus.modules.content.service.impl;
import com.angus.modules.content.domain.ENCourseQuestion;
import com.angus.modules.content.domain.ENOption;
import com.angus.modules.content.dto.EnCourseQuestionDto;
import com.angus.modules.content.repository.ENOptionRepository;
import com.angus.modules.content.repository.ENQuestionRepository;
import com.angus.modules.content.service.IEnQuestionService;
import com.angus.modules.content.vo.EnCourseQuestionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * @author liyanan
 * @date 2024/11/24 6:57 PM
 * @description
 */
@Service
public class EnQuestionServiceImpl implements IEnQuestionService {

    @Resource
    private ENQuestionRepository questionRepository;

    @Resource
    private ENOptionRepository optionRepository;

    @Override
    public List<EnCourseQuestionVo> getQuestion(EnCourseQuestionDto dto) {
        List<ENCourseQuestion> questionList = questionRepository.getQuestionList(dto.getCourseCode(), dto.getQuestionType(), dto.getGroupId(), dto.getUnitNum());
        List<Long> questionIds = questionList.stream().map(ENCourseQuestion::getId).collect(Collectors.toList());
        if(questionIds.isEmpty()){
            return new ArrayList<>();
        }
        List<ENOption> optionsByQuestionId = optionRepository.getOptionsByQuestionId(questionIds);
        Map<Long, List<ENOption>> enOptionMap = optionsByQuestionId.stream().collect(Collectors.groupingBy(ENOption::getQuestionId));
        return questionList.stream().map(question -> {
            EnCourseQuestionVo enCourseQuestionVo = new EnCourseQuestionVo();
            String questionTitle = question.getQuestionTitle().replaceAll("[\\r\\n]+", "").replaceAll("\\.", "");
            // 如果是造句类型，则随机题目
            if (dto.getQuestionType() == 0){
                String[] questionTitleArr = questionTitle.split(" ");
                List<String> questionTitleList = new ArrayList<>(Arrays.asList(questionTitleArr));
                // 移除可能的空元素（如原字符串为空）
                questionTitleList.removeIf(String::isEmpty);
                if (!questionTitleList.isEmpty()) {
                    Collections.shuffle(questionTitleList);  // 正确打乱原列表
                    String shuffledTitle = String.join(",", questionTitleList);
                    enCourseQuestionVo.setQuestionTitle(shuffledTitle);
                }else{
                    enCourseQuestionVo.setQuestionTitle(questionTitle);  // 按需设置默认值
                }
            }else{
                enCourseQuestionVo.setQuestionTitle(questionTitle);  // 按需设置默认值
            }
            enCourseQuestionVo.setCourseCode(question.getCourseCode());
            enCourseQuestionVo.setField1(question.getField1());
            enCourseQuestionVo.setReadAloud(question.getReadAloud());
            enCourseQuestionVo.setMp3Content(question.getMp3Content());
            enCourseQuestionVo.setId(question.getId());
            enCourseQuestionVo.setGroupId(question.getGroupId());
            enCourseQuestionVo.setGroupFlag(question.getGroupFlag());
            enCourseQuestionVo.setOptionType(question.getOptionType());
            List<ENOption> optionList = enOptionMap.getOrDefault(question.getId(),new ArrayList<>());
            if (optionList.size() > 0) {
                Collections.shuffle(optionList);
            }
            enCourseQuestionVo.setOptions(optionList);
            return enCourseQuestionVo;
        }).collect(Collectors.toList());
    }
}
